package com.hongye.backstage.controller;

import com.hongye.backstage.entity.HongyeCityEntity;
import com.hongye.backstage.service.HongyeCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hongye.common.enu.ResultEnum;
import com.hongye.common.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author caojianbin
 * @time 2022/4/25
 */
@RequestMapping("/backstage/city")
@RestController
public class HongyeCityController {

    @Autowired
    private HongyeCityService hongyeCityService;

    /**
     * HEAD
     * @param pid
     * @return
     * 通过pid查找出当前的下一级
     */
    @RequestMapping("/findCityByPid")
    public List<HongyeCityEntity> findCityByPid(@RequestParam("pid")Integer pid){
        return hongyeCityService.findCityByPid(pid);
    }
    /**
     * 根据父级编号查询对应的是省市区
     * @param pid 父级编号
     * @return
     */
    @RequestMapping("/getCityByPid/{pid}")
    public ResponseResult getCityByPid(@PathVariable("pid") Integer pid){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<HongyeCityEntity> entityList = hongyeCityService.getCityByPid(pid);
            result.setSuccess(ResultEnum.SUCCESS.getSuccess());
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMessage(ResultEnum.SUCCESS.getMessage());
            result.setData(entityList);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(ResultEnum.ERROR.getSuccess());
            result.setCode(ResultEnum.ERROR.getCode());
            result.setMessage(ResultEnum.ERROR.getMessage());
            return result;
        }



    }
}
